/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package reportcenter.support.resourceload;

import reportcenter.support.dbutil.*;
import org.jdom.*;
import org.jdom.input.*;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class DBDriverConfig {

    private static final String CONFIG_FILENAME = "xml/db_driver_support.xml";

    private List<DBDriver> drivers = null;
    private static final Log LOG =
            LogFactory.getLog(DBDriverConfig.class);

    public DBDriverConfig() {
        SAXBuilder builder = new SAXBuilder();

        try {

            InputStream is =
                    this.getClass().getClassLoader().getResourceAsStream(CONFIG_FILENAME);

            Document doc = builder.build(is);
            Element root = doc.getRootElement();

            for (int i = 0; i < root.getChildren().size(); i++) {
                drivers = new ArrayList<DBDriver>();
                DBDriver db = null;
                for (Object d : root.getChildren()) {
                    Element e = (Element) d;

                    db = new DBDriver();
                    db.setDescription(e.getChildTextTrim("dbDescription"));
                    db.setUripattern(e.getChildTextTrim("dbURIPattern"));
                    drivers.add(db);
                }
            }

        } catch (Exception ex) {
            LOG.error("Could not read configuration file: ", ex);
        }
    }

    public List<DBDriver> getDrivers() {
        return drivers;
    }

    public void setDrivers(List<DBDriver> drivers) {
        this.drivers = drivers;
    }

    public static void main(String[] args) {
        DBDriverConfig conf = new DBDriverConfig();
        for (DBDriver d : conf.getDrivers()) {
            System.out.println("description -> " + d.getDescription());
            System.out.println("uripattern -> " + d.getUripattern());
        }
    }
}
